//
//  DBSummary.m
//  CarCare
//
//  Created by ileo on 15/4/9.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "DBSummary.h"
#import "CalendarMgr.h"

@implementation SummaryReportModel

-(NSString *)searchDate{
    if (!_searchDate) {
        switch (self.summaryType) {
            case Summary_DB_Day:
                _searchDate = self.REPORT_DATE;
                break;
            case Summary_DB_Month:
                _searchDate = [self.REPORT_DATE substringToIndex:7];
                break;
            default:
                break;
        }
    }
    return _searchDate;
}

-(NSTimeInterval)date{
    if (_date <= 0) {
        _date = [[CALENDARMGR dateWithStr:self.REPORT_DATE format:@"yyyy-MM-dd"] timeIntervalSince1970];
    }
    return _date;
}

+(NSString *)primaryKey{
    return @"searchDate";
}

@end

@interface DBSummary()

@end

@implementation DBSummary

IMP_SINGLETON

-(void)createTable{
    self.tableName = @"DBSummary";
    [self.dataBase open];
    if (![self.dataBase tableExists:self.tableName]) {
        NSError *error = nil;
        if ([self.dataBase createTableWithName:self.tableName columns:[SummaryReportModel dbColumns] constraints:@[] error:&error]) {
        }
    }
    [self.dataBase close];
}

-(NSArray *)datas{
    self.where = nil;
    return [super datas];
}

-(NSArray *)datasWithType:(Summary_Type)type{
    self.where = [NSString stringWithFormat:@"(summaryType = %zd)",type];
    return [super datas];
}

-(void)editDatas:(NSArray *)datas{
    self.where = @"searchDate";
    [super editDatas:datas];
}

@end